//
//  CQ_FanDataDownloadTools.m
//  UILesson17_imageDownloader
//
//  Created by FanYanLong on 15/10/29.
//  Copyright © 2015年 FanYanLong. All rights reserved.
//

#import "CQ_FanDataDownloadTools.h"

@implementation CQ_FanDataDownloadTools
//实现设计方法
+(void)downloadDataWithURL:(NSString *)urlString andMethod:(NSString *)method andBody:(NSString *)body andBlock:(OptionBlock)block{
    NSURL *url = [NSURL URLWithString:urlString];
    //有没有更好的方式，忽略大小写进行比较
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    if ([method isEqualToString:@"POST"] || [method isEqualToString:@"post"]) {
        //判断结束，添加判断结果方式需要添加的东西
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    }
    //统一采用异步
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        block(data);
    }];
}
@end
